<template>
	<view class="container">
		<view style="padding:30rpx">
			<u-row>
				<u-col span="8">
					<u-input placeholder="请输入规格名" border v-model="specName"></u-input>
				</u-col>
				<u-col span="4">
					<u-button type="primary" size="medium" style="width: 100%;" @click="addSpec">添加规格</u-button>
				</u-col>
			</u-row>
		</view>
		
		<view v-for="(item,index) in specData" :key="index">
			<u-gap height="10" bg-color="#f2f2f2"></u-gap>
			<view style="padding:0 30rpx">
				<view class="specHead">
					<text class="specName">{{item.specName}}</text>
					<view style="display: flex;">
						<u-image src="@/static/shoping/trash.png" width="28" height="28" @click="delSpec(item.id)"></u-image>
						<u-image src="@/static/shoping/update.png" width="28" height="28" style="margin-left: 30rpx;" @click="editSpec(item)"></u-image>
					</view>
				</view>
				<u-row gutter="16">
					<u-col v-for="(item2,index2) in item.specDetails" :key="index2" span="4">
						<u-tag :text="item2.specDetailName" class="tags" @click="addSpecAttr(item)"></u-tag>
					</u-col>
					<u-col span="4">
						<u-icon name="plus-circle" size="50" style="margin-bottom: 20rpx;padding-bottom:12rpx" color="#a09c9c" @click="addSpecAttr(item)"></u-icon>
					</u-col>
				</u-row>
			</view>
		</view>
		<u-modal v-model="show" content="此操作将永久删除该规格名, 是否继续?" :zoom="false" show-cancel-button confirm-color="#1F6ECF" @confirm="confirm"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:false,
				productId: '',
				specData: [],
				specName:'',
				specID:''
			}
		},
		onLoad(option) {
			this.productId = option.productId;
		},
		mounted() {
			this.getData();
		},
		methods: {
			getData() {
				this.$u.api.specList({
					product_id: this.productId
				}).then(res => {
					if (res.code == 0) {
						this.specData = res.data;
					}
				})
			},
			addSpec() {
				if(this.specName){
					this.$u.api.addSpec({
						productId: this.productId,
						sortLevel: 0,
						specName: this.specName
					}).then(res => {
						if (res.code == 0) {
							this.getData();
							this.specName = '';
							this.$u.toast('规格名添加成功')
						}
					})
				}else{
					this.$u.toast('请输入规格名')
				}
				
			},
			delSpec(id){
				this.show = true;
				this.specID = id;
			},
			confirm(){
				this.$u.api.delSpec({
					id:this.specID
				}).then(res=>{
					if(res.code==0){
						this.$u.toast('删除成功');
						this.getData();
					}else{
						this.$u.toast(res.msg)
					}
				})
			},
			addSpecAttr(item){
				uni.navigateTo({
					url:"/pages/line/shoping/addSpecAttr?productId="+this.productId+'&id='+item.id+'&specName='+item.specName
				})
			},
			//编辑规格
			editSpec(item){
				uni.navigateTo({
					url:"/pages/line/shoping/editSpec?productId="+this.productId+'&id='+item.id+'&specName='+item.specName
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {

		.tags {
			width: 100%;
			text-align: center;
			margin-bottom: 20rpx;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			padding: 12rpx 4rpx;
		}

		.btn {
			background: #1F6ECF;
			box-shadow: 0px 12rpx 16rpx 0px rgba(28, 100, 224, 0.32);
			border-radius: 43rpx;
		}
		.specHead{
			margin: 30rpx 0;
			display: flex;
			justify-content: space-between;
			border-bottom: 1px solid #f2f2f2;
			.specName{
				font-size: 30rpx;
				font-weight: bold;
			}
		}
	}
</style>
